#include "dbuserwriter.h"
#include <QSqlQuery>
#include <QDebug>
DBUserWriter::DBUserWriter(QString str):AbstractUserWriter(str)
{
    this->path=str;
}

void DBUserWriter::writeUsers(QList<User *> *users)
{
    QSqlQuery query;
    if (!query.exec("DELETE FROM users"))
    {
        qDebug() << "query DELETE error";
        return;
    }
    for(int i=0; i<users->size(); i++)
    {
        //QString("insert into person values(NULL,'%1','%2',%3)")
        //.arg(firstname).arg(lastname).arg(age)
        QString name = users->at(i)->name();
        QString ids;
        for(int j=0; j<users->at(i)->cards().size(); j++)
        {
            ids+=QString::number(users->at(i)->cards().at(j))+",";

        }
        ids.remove(ids.size()-1,1);
        if (!query.exec(QString("INSERT INTO users VALUES('%1','%2')").arg(name).arg(ids)))
        {
            qDebug() << "query INSERT error";
            return;
        }
    }
}
